I am having problems with a custom data type and have wasted a lot of time now trying to get to the bottom of it and with no results so I could do with another pair of eyes.
Initially the control loads fine with all data in place as expected, on Postback I lose the data and selected value, its probably just an oversight on my part but any help would be appreciated.
My control inherits from a dropdownlist:
[code] public class BranchDropDownList : System.Web.UI.WebControls.DropDownList
{
protected override void OnInit(EventArgs e)
{
if (!Page.IsPostBack)
{
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
//- Load the branches.config file
xmldoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/data/branches.config"));
//- LINQ query to get values from branch config
this.Items.AddRange((from System.Xml.XmlElement el in xmldoc.SelectNodes("/*/branch")
select new ListItem(el.GetAttribute("name"), el.GetAttribute("id"))).ToArray());
}
base.OnInit(e);
}
}[/code]
...and my data type is as follows:
[code] ///
/// A Branch list data type
///
public class BranchDataType : AbstractDataEditor
{
#region -- Fields --
BranchDropDownList ddlBranchList = new BranchDropDownList();
#endregion Fields
#region -- Constructors --
public BranchDataType()
{
base.RenderControl = ddlBranchList;
ddlBranchList.Init += new EventHandler(ddlBranchListInit);
//- Subscribe to the DataEditorControl.OnSave event
base.DataEditorControl.OnSave += new AbstractDataEditorControl.SaveEventHandler(BranchDataTypeOnSave);
}
#endregion Constructors
private void ddlBranchListInit(object sender, EventArgs e)
{
if (base.Data.Value != null)
{
//- Set the selected branch in the list if it exists
if (this.ddlBranchList.Items.FindByValue(base.Data.Value.ToString()) != null)
{
this.ddlBranchList.Items.FindByValue(base.Data.Value.ToString()).Selected = true;
}
}
}
///
/// OnSave event handler for the BranchDataType
///
/// The instance containing the event data.
private void BranchDataTypeOnSave(EventArgs e)
{
base.Data.Value = ddlBranchList.SelectedValue;
}
#region -- Properties --
///
/// Gets the name of the data type.
///
///
public override string DataTypeName
{
get
{
return "Branch List";
}
}
///
/// Gets the unique id for the datatype.
///
///
public override Guid Id
{
get
{
return new Guid("50417222-2912-11DE-B5C6-6DAE56D89593"); ;
}
}
I now have a control that loads data in the DataBind event so the class now looks as follows:
[code] public class BranchDropDownList : System.Web.UI.WebControls.DropDownList
{
public override void DataBind()
{
this.Items.Clear();
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
//- Load the branches.config file
xmldoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/data/branches.config"));
//- LINQ query to get values from branch config
this.Items.AddRange((from System.Xml.XmlElement el in xmldoc.SelectNodes("/*/branch")
select new ListItem(el.GetAttribute("name"), el.GetAttribute("id"))).ToArray());
base.DataBind();
}
And the selected value is set when the control has been DataBound so in my BranchDataType I simply subscribe to the BranchDropDownList controls DataBound event instead of the Init event.
Custom DataType(DropDownList) Problem [SOLVED]
I am having problems with a custom data type and have wasted a lot of time now trying to get to the bottom of it and with no results so I could do with another pair of eyes.
Initially the control loads fine with all data in place as expected, on Postback I lose the data and selected value, its probably just an oversight on my part but any help would be appreciated.
My control inherits from a dropdownlist:
[code] public class BranchDropDownList : System.Web.UI.WebControls.DropDownList
{
protected override void OnInit(EventArgs e)
{
if (!Page.IsPostBack)
{
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
//- Load the branches.config file
xmldoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/data/branches.config"));
//- LINQ query to get values from branch config
this.Items.AddRange((from System.Xml.XmlElement el in xmldoc.SelectNodes("/*/branch")
select new ListItem(el.GetAttribute("name"), el.GetAttribute("id"))).ToArray());
}
base.OnInit(e);
}
}[/code]
...and my data type is as follows:
[code] ///
/// A Branch list data type
///
public class BranchDataType : AbstractDataEditor
{
#region -- Fields --
BranchDropDownList ddlBranchList = new BranchDropDownList();
#endregion Fields
#region -- Constructors --
public BranchDataType()
{
base.RenderControl = ddlBranchList;
ddlBranchList.Init += new EventHandler(ddlBranchListInit);
//- Subscribe to the DataEditorControl.OnSave event
base.DataEditorControl.OnSave += new AbstractDataEditorControl.SaveEventHandler(BranchDataTypeOnSave);
}
#endregion Constructors
private void ddlBranchListInit(object sender, EventArgs e)
{
if (base.Data.Value != null)
{
//- Set the selected branch in the list if it exists
if (this.ddlBranchList.Items.FindByValue(base.Data.Value.ToString()) != null)
{
this.ddlBranchList.Items.FindByValue(base.Data.Value.ToString()).Selected = true;
}
}
}
///
/// OnSave event handler for the BranchDataType
///
/// The instance containing the event data.
private void BranchDataTypeOnSave(EventArgs e)
{
base.Data.Value = ddlBranchList.SelectedValue;
}
#region -- Properties --
///
/// Gets the name of the data type.
///
///
public override string DataTypeName
{
get
{
return "Branch List";
}
}
///
/// Gets the unique id for the datatype.
///
///
public override Guid Id
{
get
{
return new Guid("50417222-2912-11DE-B5C6-6DAE56D89593"); ;
}
}
#endregion Properties
}[/code]
Finally I appear to have a solution.
Thanks to this post:
http://geekswithblogs.net/mikethomas/archive/2007/01/15/103686.aspx
I now have a control that loads data in the DataBind event so the class now looks as follows:
[code] public class BranchDropDownList : System.Web.UI.WebControls.DropDownList
{
public override void DataBind()
{
this.Items.Clear();
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
//- Load the branches.config file
xmldoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/data/branches.config"));
//- LINQ query to get values from branch config
this.Items.AddRange((from System.Xml.XmlElement el in xmldoc.SelectNodes("/*/branch")
select new ListItem(el.GetAttribute("name"), el.GetAttribute("id"))).ToArray());
base.DataBind();
}
protected override void EnsureDataBound()
{
try
{
if (this.RequiresDataBinding)
{
this.DataBind();
}
}
catch { }
}
}[/code]
And the selected value is set when the control has been DataBound so in my BranchDataType I simply subscribe to the BranchDropDownList controls DataBound event instead of the Init event.
Jobs good'n...
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.